var app = getApp();
const Zan = require('../../dist/index');
var Const = require('../../utils/const');

Page(Object.assign({}, Zan.TopTips, {

	data: {
		cabinet: {
			id: 0,
			name: '',
			number: '',
			useableCount: 0,
			reservableCount: 0
		},
		leftBatteries: [],
		rightBatteries: []
	},

	onLoad: function (options) {
		var that = this;
		var id = options.id;
		//var id = '974549220100935684';

		that.setData({
			"cabinet.id": id
		});

		that.getCabinet(id);
	},

	onPullDownRefresh: function () {
		var that = this;
		wx.showNavigationBarLoading();
		that.getCabinet(that.data.cabinet.id, function () {
			wx.hideNavigationBarLoading();
			wx.stopPullDownRefresh();
		});
	},

	getCabinet: function (id, fn) {
		var that = this;
		if (id) {
			app.battery.common('cabinet/detail', {
				cabinetId: id,
				ticket: app.data.token
			}).then(function (r) {
				var reservable = false;
				if (r.batteryNormalNum > 0) {
					reservable = true;
				}
				that.setData({
					cabinet: {
						id: id,
						number: r.number,
						name: r.name,
						reservable: reservable
					}
				});
				that.getCabinetBattery(r.number, fn);
			}).catch(function (e) {
				that.showZanTopTips(e.message, {type: 'error'});
				fn && fn();
			});
		} else {
			that.showZanTopTips('电池柜不存在', {type: 'error'});
			fn && fn();
		}
	},

	reserve: function (e) {
		var that = this;

		if (that.data.cabinet.reservable) {
			app.battery.common('subscribe/submit', {
				cabinetId: that.data.cabinet.id,
				ticket: app.data.token
			}).then(function (r) {
				if (r.code == 0) {
					that.showZanTopTips(r.message, {type: 'warn'});
				} else {
					that.getCabinet(that.data.cabinet.id);
					app.wechat.setStorage(Const.DATA.KEY_SUBSCRIBE_CODE, r.code);
					wx.showToast({
						title: '预约成功',
						icon: 'success',
						duration: 1000,
						success: function () {
							setTimeout(() => {
								wx.navigateBack();
							}, 1000)
						}
					});
				}
			}).catch(function (e) {
				that.showZanTopTips(e.message, {type: 'error'});
			});
		}
	},

	getCabinetBattery: function (id, fn) {
		var that = this;

		app.battery.common('cabinet/batterys', {
			cabinetNumber: id,
			ticket: app.data.token
		}).then(function (d) {
			let leftBatteries = [], rightBatteries = [], batteries = [], reservableCount = 0;
			if (d && d.length) {
				for (let i in d) {
					if (i < d.length / 2) {
						leftBatteries.push(d[i] ? (d[i].inBoxStatus != 0 ? d[i] : null) : null)
					} else {
						rightBatteries.push(d[i] ? (d[i].inBoxStatus != 0 ? d[i] : null) : null)
					}

					if (d[i]) {
						batteries.push(d[i]);
						if (d[i].useStatus <= 0 && d[i].chargerStatus == 1 && d[i].curPower >= 98) {
							reservableCount += 1;
						}
					}
				}
			}

			console.log(rightBatteries);
			that.setData({
				leftBatteries: leftBatteries,
				rightBatteries: rightBatteries
			});
			that.setData({
				"cabinet.useableCount": batteries.length,
				"cabinet.reservableCount": reservableCount
			});
			fn && fn();
		}).catch(function (e) {
			that.showZanTopTips(e.message, {type: 'error'});
			fn && fn();
		});
	}
}));